<!-- 登录页面 -->
<template>
  <div class="login-wrapper">
    <div class="login-content">
      <div class="logo-info">
        <img src="../../assets/vueImage.png" />
        <p>Vue2 Admin Project</p>
        <span>Vue2 Admin Project 是一款中/后台管理系统</span>
      </div>
      <el-form :model="loginForm" :hide-required-asterisk="true" :rules="rules" ref="loginForm" class="loginForm">
        <el-form-item label="" prop="username">
          <el-input type="text" prefix-icon="el-icon-user" placeholder="请输入账号" clearable
            v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" placeholder="请输入密码" clearable show-password
            v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" style="width: 100%"
            @click="onSubmit('loginForm')">登录</el-button>
        </el-form-item>
      </el-form>
      <div class="other-login">
        <el-button text disabled>其它登录方式</el-button>
        <el-button text disabled>忘记密码</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Vue2AdminProjectLogin',

  data () {
    return {
      btnLoading: false,
      loginForm: {
        username: 'admin',
        password: '123456',
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
    }
  },

  created () {
    this.keyupSubmit()
  },

  mounted () { },

  methods: {
    onSubmit (formName) {
      // 为表单绑定验证功能
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log('submit!')
          console.log(this.loginForm.username)
          console.log(this.loginForm.password)
          this.btnLoading = true
          const userObj = JSON.stringify({
            userName: this.loginForm.username,
            passWord: this.loginForm.password,
          })
          window.sessionStorage.setItem('user', userObj)
          // 跳转到系统首页
          this.$router.replace({ path: '/home' })
          this.btnLoading = false
          // })
        } else {
          this.btnLoading = false
          console.log('error submit!!')
          return false
        }
      })
    },

    // 监听键盘回车事件
    keyupSubmit () {
      document.onkeydown = e => {
        // console.log('[ e ]', e)
        let _key = e.key
        if (_key === 'Enter') {
          this.onSubmit('loginForm')
        }
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.login-wrapper {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url(../../assets/loginImage.jpg);

  .login-content {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    overflow: hidden;
    padding: 0 26px 24px 26px;
    box-sizing: border-box;
    margin-left: -198px;
    margin-top: -255px;

    .logo-info {
      padding: 12px;
      text-align: center;

      img {
        height: 55px;
      }

      p {
        font-size: 24px;
        padding: 2px 0px;
      }

      span {
        font-size: 14px;
        color: #909399;
      }
    }

    .other-login {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
